/*
 * ADC.c
 *
 * Created: 12.09.2012 16:42:28
 *  Author: mariuvi
 */ 
#include <avr/io.h>
#include <avr/interrupt.h>
#include "definitions.h"
#include "ADC.h"

volatile uint8_t ch_0 = 255;

void ADC_init()
{
	cli();
	//Set VREF = 5V 
	BIT_SET(ADMUX,REFS0);
	
	//Set result left adjusted, only need to read ADCH (8 bit resolution)
	BIT_SET(ADMUX, ADLAR);
	
	//Starts with channel 0
	BIT_SET(ADMUX, MUX0);
	
	//Enable ADC
	BIT_SET(ADCSRA, ADEN);
	
	//Enable conversion complete interrupt
	BIT_SET(ADCSRA, ADIE);
	
	//Enable Free running
	BIT_SET(ADCSRA, ADFR);
	
	//Set prescaler 128
	BIT_SET(ADCSRA, ADPS2);
	BIT_SET(ADCSRA, ADPS1);
	BIT_SET(ADCSRA, ADPS0);
	
	
	BIT_SET(ADCSRA, ADSC);
	sei();
}

int ADC_read()
{
	return ch_0;
}


ISR(ADC_vect)
{
	ch_0 = ADCH;		
}
